package com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel

import androidx.databinding.ObservableArrayList
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.core.databinding.IntObservableField
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.GoldRecordListDto
import com.vitabuybuy.tv.network.TalentWalletRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class LiveIncomeManageViewModel : BaseViewModel() {

    val myWallet = IntObservableField()

    val recordList = ObservableArrayList<GoldRecordListDto>()

    val recordBinding: OnItemBind<GoldRecordListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: GoldRecordListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_live_income_record)
                .bindExtra(BR.position, position)
        }

    fun getData(
        type: String?,
        pageNumber: Int
    ) {
        rxHttpRequest {
            onRequest = {
                val it = TalentWalletRepository.getGoldRecordList(type, pageNumber, 10).await()
                if (pageNumber == 1) {
                    recordList.clear()
                }
                recordList.addAll(it)
                if (recordList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun getMyWallet() {
        rxHttpRequest {
            onRequest = {
                myWallet.set(TalentWalletRepository.getMyGoldWallet().await())
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun withDrawQuantity(drawQuantity: Int) {
        rxHttpRequest {
            onRequest = {
                TalentWalletRepository.withDrawQuantity(drawQuantity).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
